<template>
  <div>
    <login-logo/>
    <div class="signUp">
      <div class="signUp-head">
        <span>用户登录</span>
      </div>
      <el-form :model="registerForm" ref="registerForm" label-width="70px" class="demo-ruleForm" :rules="rules"
               size="mini">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="registerForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" size="mini">
          <el-input v-model="registerForm.password" placeholder="密码"></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button @click="goSignUp" size="mini" style="width: 100px;margin-left: 50px;border-radius:5px;">注册
          </el-button>
          <el-button type="primary" @click="handleLoginIn" size="mini"
                     style="width: 100px;margin-right: 50px;border-radius:5px;">登录
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import loginLogo from '../components/loginLogo'
import {rules} from '@/assets/data/form'
import {mixin} from "@/mixins";
import {LoginIn} from '../api/index'


export default {
  name: "login-in",
  mixins: [mixin],
  components: {
    loginLogo
  },
  data() {
    return {
      registerForm: {
        username: '',//用户名
        password: '',//密码
      },
      rules: {}, //表单提交的规则
    }
  },
  created() {
    this.rules = rules;
    console.log('loginIn', JSON.parse(localStorage.getItem('loginIn')))
  },
  mounted() {
    this.changeIndex('登录')
  },
  methods: {
    //登录
    handleLoginIn() {
      let _this = this;
      let params = {
        ...this.registerForm,
      }
      LoginIn(params).then(res => {
        if (res.code === 200) {
          _this.$store.commit('setSConsumer', res.data);
          _this.$bus.$emit('setSConsumer', res.data)
          _this.notify('登录成功', 'success');
          _this.$store.commit('setLoginIn', true);
          setTimeout(function () {
            _this.changeIndex('首页');
            _this.$router.push({path: '/'});
          }, 30);
        } else {
          _this.notify('登录失败', 'error')
        }
      }).catch(error => {
        console.log(error)
        _this.notify('登录失败', 'error');
      })
    },
    //注册
    goSignUp() {
      this.changeIndex('注册')
      this.$router.push({path: '/sign-up'})
    },
    changeIndex(value) {
      this.$store.commit('setActiveName', value)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../assets/css/sign-up.scss";
</style>
